class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ result = [[]] for num in sorted(nums): result += [item + [num] for item in result] return result
class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ self.result = [] nums.sort() for i in range(0, len(nums)+1): self.k = i self.combineHelper(0, [], nums) return self.result
def combineHelper(self, length, temp, list_num): if length == self.k: self.result.append(temp[:]) # 需要用切片浅复制出一个新数组 return for i, num in enumerate(list_num): temp.append(num) self.combineHelper(length+1, temp, list_num[i+1:]) temp.pop()
class Solution(object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ result = [[]] nums.sort() # sort()直接修改原数组,sorted()返回排序后的数组,不修改原数组 temp_size = 0 for i in range(len(nums)): start = temp_size if i >= 1 and nums[i] == nums[i - 1] else 0 temp_size = len(result) # 每次取到result最后一位,也就是从start一直到最后一位 for j in range(start, temp_size): result.append(result[j] + [nums[i]]) print result return result